package com.rain.jwt.web;

import com.alibaba.fastjson.JSON;
import com.rain.jwt.common.Result;
import com.rain.jwt.config.Auth;
import com.rain.jwt.dto.PayloadDTO;
import com.rain.jwt.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * @author: wu
 * @date: 2021/9/25
 */
@RestController
@RequestMapping(value="/user")
@Api(tags = "用户")
public class UserController {


    @PostMapping(value = "/login")
    @Auth(token = false)
    @ApiOperation("登录")
    public Result<String> login(String username,String password) {
        // 用户常规校验
        Long userId = 100L;
        // 用户信息存入缓存
        // 生成token
        String token = JwtUtil.generate(JwtUtil.getDefaultPayload(userId));
        return Result.success(token);
    }

    @GetMapping(value = "refreshToken")
    @Auth
    @ApiOperation("刷新token")
    public Result<String> refreshToken(String token) {
        String freshToken = JwtUtil.freshToken(token);
        return Result.success(freshToken);
    }

    @GetMapping(value = "test")
    @Auth
    @ApiOperation("测试")
    public Result<String> test() {
        return Result.success("测试成功");
    }


}
